package com.water_work_form.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deepoove.poi.XWPFTemplate;
import com.water_work_form.entity.BasicRestriction;
import com.water_work_form.entity.RecordOrganoleptic;
import com.water_work_form.mapper.*;
import com.water_work_form.service.RecordOrganolepticService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 记录——感官性状指标原始记录 服务实现类
 * </p>
 *
 * @author xuke
 * @since 2023-09-14
 */
@Service
public class RecordOrganolepticServiceImpl extends ServiceImpl<RecordOrganolepticMapper, RecordOrganoleptic> implements RecordOrganolepticService {

    @Autowired
    RecordOrganolepticMapper recordOrganolepticMapper;

    @Autowired
    BasicFormMapper basicFormMapper;

    @Autowired
    BasicRestrictionMapper basicRestrictionMapper;

    @Autowired
    BasicDeviceMapper basicDeviceMapper;

    @Autowired
    BasicDrugMapper basicDrugMapper;

    @Value("${file.templateFilePath}")
    private String templateFilePath;

    @Value("${file.destFilePath}")
    private String destFilePath;

    /**
     * 记录——感官性状指标原始记录 项目查询（三个查询项+分页+排序）
     * @param checkUser 检测人（check_user_name） 若为空则不进行此项查询，否则进行模糊查询
     * @param reviewUser 审核人（review_user_name） 若为空则不进行此项查询，否则进行模糊查询
     * @param status 项目状态（status） 若为空则不进行此项查询，否则返回对应项
     * @param current 分页查询 当前页数
     * @param size 分页查询 每页最大数据量
     * @return 返回相应数据 按照状态（status）升序排序
     */
    @Override
    public IPage<RecordOrganoleptic> selectRecordOrganoleptic(String checkUser, String reviewUser, String status, Integer current, Integer size){
        QueryWrapper<RecordOrganoleptic> queryWrapper = new QueryWrapper<>();
        if(!checkUser.isEmpty()){
            queryWrapper.like("check_user_name", checkUser);
        }
        if(!reviewUser.isEmpty()){
            queryWrapper.like("review_user_name", reviewUser);
        }
        if(!status.isEmpty()){
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByAsc("status");
        IPage<RecordOrganoleptic> page = new Page<>(current, size);
        return this.page(page,queryWrapper);
    }

    /**
     * 记录——感官性状指标原始记录 基础表数据查询
     * @param name form表name
     * @param restrictionName6 restriction表name
     * @param restrictionName7 restriction表name
     * @param restrictionName8 restriction表name
     * @param restrictionName9 restriction表name
     * @param restrictionName10 restriction表name
     * @param restrictionName11 restriction表name
     * @param drugName drug表name
     * @return 返回对应数据（除restriction表后编号对应查询项对应编号外，其余表键名为表名）
     */
    @Override
    public Map<String,Object> selectBasicOrganoleptic(String name, String restrictionName6, String restrictionName7, String restrictionName8, String restrictionName9, String restrictionName10,
                                                      String restrictionName11, String drugName) {
        Map<String,Object> map = new HashMap<>();
        map.put("form", basicFormMapper.getOneByName(name));
        QueryWrapper<BasicRestriction> queryWrapper = new QueryWrapper<>();
        map.put("restriction", basicRestrictionMapper.selectList(queryWrapper
                .eq("name", restrictionName6).or().eq("name", restrictionName7).or().eq("name", restrictionName8).or()
                .eq("name", restrictionName9).or().eq("name", restrictionName10).or().eq("name", restrictionName11)
                .select("name", "check_content", "analysis_method", "standard_basis")));
        map.put("drug", basicDrugMapper.getOneByName(drugName));
        return map;
    }

    /**
     * 记录——感官性状指标原始记录 数据表导出
     * @param recordOrganoleptic 需要导出的数据表
     * @throws IOException 异常
     */
    @Override
    public void downloadRecordOrganoleptic(RecordOrganoleptic recordOrganoleptic) throws IOException {
        Map<String, Object> data = new HashMap<String, Object>();
        JSONObject turbidity =  JSON.parseObject(recordOrganoleptic.getTurbidity());
        JSONObject temperature =  JSON.parseObject(recordOrganoleptic.getTemperature());
        JSONObject chrominance =  JSON.parseObject(recordOrganoleptic.getChrominance());
        JSONObject odorTasteCode =  JSON.parseObject(recordOrganoleptic.getOdorTasteCode());
        JSONObject odorTasteHot =  JSON.parseObject(recordOrganoleptic.getOdorTasteHot());
        JSONObject visible =  JSON.parseObject(recordOrganoleptic.getVisible());
        data.put("turbidity", turbidity);
        data.put("temperature", temperature);
        data.put("chrominance", chrominance);
        data.put("odorTasteCode", odorTasteCode);
        data.put("odorTasteHot", odorTasteHot);
        data.put("visible", visible);
        data.put("list", recordOrganoleptic);
        XWPFTemplate compile = XWPFTemplate.compile(templateFilePath + "RecordOrganoleptic.docx");
        compile.render(data);
        compile.writeToFile(destFilePath + recordOrganoleptic.getId() + "out_recordOrganoleptic.docx");
        compile.close();
    }


}
